(function($){
	$.fn.Tabs=function(speed){
		return this.each(function(){
			var tabs=$(this),
				tabsLabels=tabs.children("ul"),
				tabLabel=tabsLabels.find("a"),
				tabsContent=tabs.children("div"),
				tabLink,
				tabsLoader=tabsContent.children(".ajax-loader"); 
			if(!speed){
				speed=0; 
			}
			if(tabsContent.length===0){
				tabs.append("<div />");
				tabsContent=tabs.children("div");
				tabsLabels.children("li").each(function(){
					tabsContent.append("<div class='ajax-tab-content' />");
				});
			}else{
				tabsContent.children("div").each(function(){
					if($.trim($(this).text())===""){
						$(this).addClass("ajax-tab-content");
					}
				});
			}
			if(tabsLoader.length===0 && tabsContent.children("div.ajax-tab-content").length>0){
				tabsContent.append("<span class='ajax-loader' />");
				tabsLoader=tabsContent.children(".ajax-loader");
			}
			if(tabsLabels.find(".active-tab").length===0){
				tabsLabels.children("li:first").addClass("active-tab");
			}
			tabLink=tabsLabels.find(".active-tab a");
			tabsContent.css({"height":tabsContent.height()}); 
			LoadTabContent(FindTabContent(tabLink),tabLink);
			tabLabel.click(function(e){
				e.preventDefault();
				tabLink=$(this);
				LoadTabContent(FindTabContent(tabLink),tabLink);
			});
			function FindTabContent(tabLink){ 
				var targetTab,
					tabID=tabLink.attr("href");
				if(tabID && tabID!="#" && tabID.substring(0)==="#" && tabsContent.find(tabID).length!=0){
					targetTab=tabsContent.find(tabID);
				}else{
					tabID=tabLink.parent().index();
					targetTab=tabsContent.children("div").eq(tabID);
				}
				return targetTab;
			}
			function LoadTabContent(targetTab,tabLink){
				var activeTab=tabsLabels.find(".active-tab"),
					activeTabContent=tabsContent.find(".active-tab-content");
				if(activeTabContent.length===0){
					activeTabContent=targetTab;
				}
				activeTabContent.stop().fadeOut(speed,function(){ 
					activeTab.add(activeTabContent).removeClass("active-tab active-tab-content"); 
					tabLink.parent().addClass("active-tab"); 
					if(targetTab.is(".ajax-tab-content")){
						tabsLoader.stop().fadeIn(speed); 
						targetTab.load(tabLink.attr("href"),function(){
							var targetTabImages=targetTab.find("img"),
								targetTabImagesLoaded=0;
							if(targetTabImages.length>0){
								targetTabImages.bind("load",function(){
									targetTabImagesLoaded++
									if(targetTabImagesLoaded>=targetTabImages.length){
										tabsLoader.stop().fadeOut(speed);
										ShowTabContent();
									}
								});
							}else{
								tabsLoader.stop().fadeOut(speed);
								ShowTabContent();
							}
						});
					}else{
						ShowTabContent();
					}
					function ShowTabContent(){
						targetTab.addClass("active-tab-content").css({"opacity":0,"display":"block"});
						tabsContent.stop().animate({height:targetTab.outerHeight()},speed,function(){ 
							targetTab.stop().animate({opacity:1},speed); 
						});
					}
				});
			}
		});
	}
})(jQuery); 